package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Travel;
import com.itheima.service.FoodService;
import com.itheima.service.TravelService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/travel")
public class TravelController {

    @Reference
    private TravelService travelService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return travelService.findPage(queryPageBean);
    }


    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        try {
            List<String[]> list = POIUtils.readExcel(file);
            if (list != null && list.size() > 0) {
                ArrayList<Travel> travels = new ArrayList<>();
                for (String[] strings : list) {
                    String code = strings[1];
                    String name = strings[2];
                    String travelTimes = strings[6];
                    String travelTime = strings[5];

                    Travel travel = new Travel(code,name,travelTimes,travelTime);
                    travels.add(travel);
                }
                travelService.add(travels);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

}
